package com.snpic.appaw.module.insure.controller.app.cxOrder;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.snpic.appaw.framework.common.pojo.PageParam;
import com.snpic.appaw.framework.common.pojo.PageResult;
import com.snpic.appaw.framework.common.pojo.CommonResult;
import com.snpic.appaw.framework.common.util.object.BeanUtils;
import static com.snpic.appaw.framework.common.pojo.CommonResult.success;

import com.snpic.appaw.framework.excel.core.util.ExcelUtils;

import com.snpic.appaw.framework.apilog.core.annotation.ApiAccessLog;
import static com.snpic.appaw.framework.apilog.core.enums.OperateTypeEnum.*;

import com.snpic.appaw.module.insure.controller.app.cxOrder.vo.*;
import com.snpic.appaw.module.insure.dal.dataobject.cxOrder.CxOrderDO;
import com.snpic.appaw.module.insure.service.cxOrder.CxOrderService;

@Tag(name = "用户 APP - 车险订单")
@RestController
@RequestMapping("/insure/cx-order")
@Validated
public class AppCxOrderController {

    @Resource
    private CxOrderService cxOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建车险订单")
    public CommonResult<Long> createCxOrderDO(@Valid @RequestBody AppCxOrderDOSaveReqVO createReqVO) {
        return success(cxOrderService.createCxOrderDO(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车险订单")
    public CommonResult<Boolean> updateCxOrderDO(@Valid @RequestBody AppCxOrderDOSaveReqVO updateReqVO) {
        cxOrderService.updateCxOrderDO(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车险订单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteCxOrderDO(@RequestParam("id") Long id) {
        cxOrderService.deleteCxOrderDO(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除车险订单")
    public CommonResult<Boolean> deleteCxOrderDOList(@RequestParam("ids") List<Long> ids) {
        cxOrderService.deleteCxOrderDOListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车险订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppCxOrderDORespVO> getCxOrderDO(@RequestParam("id") Long id) {
        CxOrderDO cxOrderDO = cxOrderService.getCxOrderDO(id);
        return success(BeanUtils.toBean(cxOrderDO, AppCxOrderDORespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车险订单分页")
    public CommonResult<PageResult<AppCxOrderDORespVO>> getCxOrderDOPage(@Valid AppCxOrderDOPageReqVO pageReqVO) {
        PageResult<CxOrderDO> pageResult = cxOrderService.getCxOrderDOPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppCxOrderDORespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出车险订单 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCxOrderDOExcel(@Valid AppCxOrderDOPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CxOrderDO> list = cxOrderService.getCxOrderDOPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车险订单.xls", "数据", AppCxOrderDORespVO.class,
                        BeanUtils.toBean(list, AppCxOrderDORespVO.class));
    }

}